package com.markor.tx.service.impl;

import com.markor.tx.bean.Family;
import com.markor.tx.bean.People;
import com.markor.tx.mapper.PeopleMapper;
import com.markor.tx.service.FamilyService;
import com.markor.tx.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author:zhangyanan
 * @Description: 此类为讲解spring事务的传播机制案例
 * @Date:Crated in 10:05 2018/3/14
 * @Modified By:
 */

@Service
public class PeopleServiceImpl implements PeopleService {

//    @Autowired
//    private PeopleMapper peopleMapper;

    @Autowired
    private FamilyService familyService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 详情请查看familyServiceImpl事务的传播机制注释
     * @param people
     * @param family
     */
    public void insert(People people, Family family) {

        //虽然在方法上没有标注@Transactional注解，但mybatis会自动提交事务，因为没有事务，所以会执行一句，提交一句
//        peopleMapper.insert(people);

        jdbcTemplate.update("insert into MK_PEOPLE (AGE,PEOPLE_NAME) values (?,?)",people.getAge(),people.getPeopleName());

        /**
         * 当前无事务，创建新事务
         */
        familyService.insertByRequired(family);
        //此时不属于同一个事务，如果抛出异常，family会插入成功
        System.out.println(1/0);

        /**
         * 如果当前无事务，则创建新事务
         */
        /*familyService.insertByRequiresNew(family);*/

        /**
         * 当前无事务，以非事务方式运行
         */
        /*familyService.insertBySupports(family);*/

        /**
         * 以非事务运行
         */
        /*familyService.insertByNotSupported(family);*/

        /**
         * 以非事务运行
         */
        /*familyService.insertByNever(family);*/

        /**
         * 当前无事务，抛出异常
         */
        /*familyService.insertByMandatory(family);*/

        /**
         * 当前无事务,该取值等价于TransactionDefinition.PROPAGATION_REQUIRED。
         */
        /*familyService.insertByNested(family);*/
    }



}
